package com.ruoyi.flowable.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.domain.WfDeployForm;
import com.ruoyi.flowable.domain.WfForm;
import com.ruoyi.flowable.service.IWfDeployFormService;
import com.ruoyi.flowable.service.IWfFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 流程单信息Controller
 *
 * @author siashan
 * @date 2023-01-03
 */
@RestController
@RequestMapping("/flowable/form")
public class WfFormController extends BaseController {

    @Autowired
    private IWfFormService wfFormService;
    @Autowired
    private IWfDeployFormService wfDeployFormService;
    /**
     * 查询流程单信息列表
     *
     * @param wfForm 参数
     * @return 列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:form:list')")
    @GetMapping("/list")
    public TableDataInfo list(WfForm wfForm) {
        startPage();
        LambdaQueryWrapper<WfForm> lqw = new LambdaQueryWrapper<WfForm>();
        if (StringUtils.isNotBlank(wfForm.getFormName())) {
            lqw.like(WfForm::getFormName, wfForm.getFormName());
        }
        List<WfForm> list = wfFormService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出流程单信息列表
     *
     * @param wfForm
     * @return 导出结果
     */
    @PreAuthorize("@ss.hasPermi('flowable:form:export')")
    @Log(title = "流程单信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WfForm wfForm) {
        LambdaQueryWrapper<WfForm> lqw = new LambdaQueryWrapper<WfForm>(wfForm);
        List<WfForm> list = wfFormService.list(lqw);
        ExcelUtil<WfForm> util = new ExcelUtil<WfForm>(WfForm.class);
        return util.exportExcel(list, "form");
    }

    /**
     * 获取流程单信息详细信息
     *
     * @param formId id
     * @return 详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowable:form:query')")
    @GetMapping(value = "/{formId}")
    public AjaxResult getInfo(@PathVariable("formId") Long formId) {
        return success(wfFormService.getById(formId));
    }

    /**
     * 新增流程单信息
     *
     * @param wfForm 参数
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('flowable:form:add')")
    @Log(title = "流程单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WfForm wfForm) {
        return toAjax(wfFormService.save(wfForm) ? 1 : 0);
    }

    /**
     * 修改流程单信息
     *
     * @param wfForm 参数
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('flowable:form:edit')")
    @Log(title = "流程单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WfForm wfForm) {
        return toAjax(wfFormService.updateById(wfForm) ? 1 : 0);
    }

    /**
     * 删除流程单信息
     *
     * @param formIds 主键id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('flowable:form:remove')")
    @Log(title = "流程单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{formIds}")
    public AjaxResult remove(@PathVariable Long[] formIds) {
        return toAjax(wfFormService.removeByIds(Arrays.asList(formIds)) ? 1 : 0);
    }

    /**
     * 挂载流程表单
     */
    @Log(title = "流程表单", businessType = BusinessType.INSERT)
    @PostMapping("/addDeployForm")
    public AjaxResult addDeployForm(@RequestBody WfDeployForm deployForm) {
        return toAjax(wfDeployFormService.insertWfDeployForm(deployForm));
    }
}
